package AdminControl;

import java.io.IOException;
import java.util.ArrayList;
import Model.Mobile;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * Servlet implementation class MobileSearch
 */
public class MobileSearch extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MobileSearch() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		 response.setContentType("text/html;charset=UTF-8");

	        String manufacturer_id = request.getParameter("manufacturer");
	        String keyword = request.getParameter("keyword");
	        manufacturer_id = (manufacturer_id == null)?"":manufacturer_id;
	        keyword = (keyword == null)?"":keyword;

	        String page_s = request.getParameter("page");
	        page_s = (page_s == null)?"1":page_s;
	        int page_i=1;
	        try{
	            page_i = Integer.parseInt(page_s);
	        }catch(Exception ex){
	        }
	        

	        ArrayList<Mobile> listResult = Mobile.searchMobile(keyword, manufacturer_id, page_i);
	        int numOfRecord = Mobile.countRecord(keyword, manufacturer_id);
	        int numOfPage = (numOfRecord%20 == 0)?(numOfRecord/20):(numOfRecord/20 +1);

	        request.setAttribute("listResult", listResult);
	        request.setAttribute("numOfPage", numOfPage);
	        RequestDispatcher re = getServletContext().getRequestDispatcher("/Admin/ProductManage.jsp");
	        re.forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
